﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace CarolLib.PostCrawler.Model
{
    /// <summary>
    /// 类t_Post。
    /// </summary>
    [Serializable]
    public partial class TPost
    {
        public TPost()
        { }

		/// <summary>
		/// Post OR映射构造函数
		/// </summary>
		public TPost(System.Data.IDataReader dr)
		{
			this.PostID = (Guid)dr["PostID"];
			this.ThreadID = (Guid)dr["ThreadID"];
			this.TypeID = (Guid)dr["TypeID"];
			this.PostNo = Convert.ToInt32(dr["PostNo"]);
			this.PostTitle = Convert.ToString(dr["PostTitle"]);
			this.PostContent = Convert.ToString(dr["PostContent"]);
			this.PostTime = Convert.ToDateTime(dr["PostTime"]);
		}

        #region Model
        private Guid _postid;
        private Guid _threadid;
        private Guid _typeid;
        private int _postno;
        private string _posttitle = "";
        private string _postcontent = "";
        private DateTime _posttime;
        /// <summary>
        /// 
        /// </summary>
        public Guid PostID
        {
            set { _postid = value; }
            get { return _postid; }
        }
        /// <summary>
        /// 
        /// </summary>
        public Guid ThreadID
        {
            set { _threadid = value; }
            get { return _threadid; }
        }
        /// <summary>
        /// 
        /// </summary>
        public Guid TypeID
        {
            set { _typeid = value; }
            get { return _typeid; }
        }
        /// <summary>
        /// 
        /// </summary>
        public int PostNo
        {
            set { _postno = value; }
            get { return _postno; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string PostTitle
        {
            set { _posttitle = value; }
            get { return _posttitle; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string PostContent
        {
            set { _postcontent = value; }
            get { return _postcontent; }
        }
        /// <summary>
        /// 
        /// </summary>
        public DateTime PostTime
        {
            set { _posttime = value; }
            get { return _posttime; }
        }

        public string Url { get; set; }

        #endregion Model

        #region  Method

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public TPost(Guid PostID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select PostID,ThreadID,TypeID,PostNo,PostTitle,PostContent,PostTime ");
            strSql.Append(" FROM [t_Post] ");
            strSql.Append(" where PostID=@PostID ");
            SqlParameter[] parameters = {
					new SqlParameter("@PostID", SqlDbType.UniqueIdentifier)};
            parameters[0].Value = PostID;

            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["PostID"] != null && ds.Tables[0].Rows[0]["PostID"].ToString() != "")
                {
                    this.PostID = Guid.Parse(ds.Tables[0].Rows[0]["PostID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ThreadID"] != null && ds.Tables[0].Rows[0]["ThreadID"].ToString() != "")
                {
                    this.ThreadID = Guid.Parse(ds.Tables[0].Rows[0]["ThreadID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["TypeID"] != null && ds.Tables[0].Rows[0]["TypeID"].ToString() != "")
                {
                    this.TypeID = Guid.Parse(ds.Tables[0].Rows[0]["TypeID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["PostNo"] != null && ds.Tables[0].Rows[0]["PostNo"].ToString() != "")
                {
                    this.PostNo = int.Parse(ds.Tables[0].Rows[0]["PostNo"].ToString());
                }
                if (ds.Tables[0].Rows[0]["PostTitle"] != null && ds.Tables[0].Rows[0]["PostTitle"].ToString() != "")
                {
                    this.PostTitle = ds.Tables[0].Rows[0]["PostTitle"].ToString();
                }
                if (ds.Tables[0].Rows[0]["PostContent"] != null && ds.Tables[0].Rows[0]["PostContent"].ToString() != "")
                {
                    this.PostContent = ds.Tables[0].Rows[0]["PostContent"].ToString();
                }
                if (ds.Tables[0].Rows[0]["PostTime"] != null && ds.Tables[0].Rows[0]["PostTime"].ToString() != "")
                {
                    this.PostTime = DateTime.Parse(ds.Tables[0].Rows[0]["PostTime"].ToString());
                }
            }
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int PostNo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from [t_Post]");
            strSql.Append(" where PostNo=@PostNo ");

            SqlParameter[] parameters = {
					new SqlParameter("@PostNo", SqlDbType.Int,4)};
            parameters[0].Value = PostNo;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into [t_Post] (");
            strSql.Append("ThreadID,TypeID,PostNo,PostTitle,PostContent,PostTime,Url)");
            strSql.Append(" values (");
            strSql.Append("@ThreadID,@TypeID,@PostNo,@PostTitle,@PostContent,@PostTime,@Url)");
            SqlParameter[] parameters = {
					new SqlParameter("@ThreadID", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@TypeID", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@PostNo", SqlDbType.Int,4),
					new SqlParameter("@PostTitle", SqlDbType.NVarChar,50),
					new SqlParameter("@PostContent", SqlDbType.NVarChar),
					new SqlParameter("@PostTime", SqlDbType.DateTime),
					new SqlParameter("@Url", SqlDbType.NVarChar,150)};
            parameters[0].Value = ThreadID;
            parameters[1].Value = TypeID;
            parameters[2].Value = PostNo;
            parameters[3].Value = PostTitle;
            parameters[4].Value = PostContent;
            parameters[5].Value = PostTime;
            parameters[6].Value = Url;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update [t_Post] set ");
            strSql.Append("PostID=@PostID,");
            strSql.Append("ThreadID=@ThreadID,");
            strSql.Append("TypeID=@TypeID,");
            strSql.Append("PostNo=@PostNo,");
            strSql.Append("PostTitle=@PostTitle,");
            strSql.Append("PostContent=@PostContent,");
            strSql.Append("PostTime=@PostTime");
            strSql.Append(" where PostID=@PostID ");
            SqlParameter[] parameters = {
					new SqlParameter("@PostID", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@ThreadID", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@TypeID", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@PostNo", SqlDbType.Int,4),
					new SqlParameter("@PostTitle", SqlDbType.NVarChar,50),
					new SqlParameter("@PostContent", SqlDbType.NVarChar),
					new SqlParameter("@PostTime", SqlDbType.DateTime)};
            parameters[0].Value = PostID;
            parameters[1].Value = ThreadID;
            parameters[2].Value = TypeID;
            parameters[3].Value = PostNo;
            parameters[4].Value = PostTitle;
            parameters[5].Value = PostContent;
            parameters[6].Value = PostTime;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(Guid PostID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [t_Post] ");
            strSql.Append(" where PostID=@PostID ");
            SqlParameter[] parameters = {
					new SqlParameter("@PostID", SqlDbType.UniqueIdentifier)};
            parameters[0].Value = PostID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public void GetModel(Guid PostID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select PostID,ThreadID,TypeID,PostNo,PostTitle,PostContent,PostTime ");
            strSql.Append(" FROM [t_Post] ");
            strSql.Append(" where PostID=@PostID ");
            SqlParameter[] parameters = {
					new SqlParameter("@PostID", SqlDbType.UniqueIdentifier)};
            parameters[0].Value = PostID;

            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["PostID"] != null && ds.Tables[0].Rows[0]["PostID"].ToString() != "")
                {
                    this.PostID = new Guid(ds.Tables[0].Rows[0]["PostID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ThreadID"] != null && ds.Tables[0].Rows[0]["ThreadID"].ToString() != "")
                {
                    this.ThreadID = new Guid(ds.Tables[0].Rows[0]["ThreadID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["TypeID"] != null && ds.Tables[0].Rows[0]["TypeID"].ToString() != "")
                {
                    this.TypeID = new Guid(ds.Tables[0].Rows[0]["TypeID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["PostNo"] != null && ds.Tables[0].Rows[0]["PostNo"].ToString() != "")
                {
                    this.PostNo = int.Parse(ds.Tables[0].Rows[0]["PostNo"].ToString());
                }
                if (ds.Tables[0].Rows[0]["PostTitle"] != null && ds.Tables[0].Rows[0]["PostTitle"].ToString() != "")
                {
                    this.PostTitle = ds.Tables[0].Rows[0]["PostTitle"].ToString();
                }
                if (ds.Tables[0].Rows[0]["PostContent"] != null && ds.Tables[0].Rows[0]["PostContent"].ToString() != "")
                {
                    this.PostContent = ds.Tables[0].Rows[0]["PostContent"].ToString();
                }
                if (ds.Tables[0].Rows[0]["PostTime"] != null && ds.Tables[0].Rows[0]["PostTime"].ToString() != "")
                {
                    this.PostTime = DateTime.Parse(ds.Tables[0].Rows[0]["PostTime"].ToString());
                }
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<TPost> GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM [t_Post] ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }

            using (var dr = DbHelperSQL.ExecuteReader(strSql.ToString()))
            {
                List<TPost> result = new List<TPost>();

                while (dr.Read())
                {
                    result.Add(new TPost(dr));
                }

                dr.Close();
                return result;
            }
        }

        #endregion  Method
    }
}
